package com.cjb.cloudsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.DeviceStatus;
import com.cjb.cloudsystem.mapper.DeviceStatusMapper;
import com.cjb.cloudsystem.service.DeviceStatusService;
import com.cjb.cloudsystem.vo.DeviceStatusVO;
import com.cjb.common.exception.ServerException;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.Common;
import com.cjb.common.util.Constant;
import com.cjb.common.util.JsonUtils;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Author chenjinbin
* @Package IntelliJ IDEA
* @Class DeviceStatussServiceImpl
* @Date 2023-07-27 21:41
*/
@Service
@AllArgsConstructor
public class DeviceStatusServiceImpl extends BaseRepositoryImpl<DeviceStatusMapper, DeviceStatus> implements DeviceStatusService {
/**
* 日志
*/
    private final static Logger log = LoggerFactory.getLogger(DeviceStatusServiceImpl.class);
    /*
    * 注入mapper层
    */
    private final DeviceStatusMapper deviceStatusMapper;
    /**
    * 单体保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    @Transactional
    public boolean save(DeviceStatus deviceStatus) {
        if (Common.isEmpty(deviceStatus)) {
            throw new ServerException("sysObject is empty");
        }
        if (Common.isEmpty(deviceStatus.getIsDel())) {
            deviceStatus.setIsDel(Constant.NO);
        }
        if (Common.isEmpty(deviceStatus.getTemperatureStatus())){
            deviceStatus.setTemperatureStatus(Constant.NO);
        }
        if (Common.isEmpty(deviceStatus.getBrightnessStatus())){
            deviceStatus.setBrightnessStatus(Constant.NO);
        }
        if (Common.isEmpty(deviceStatus.getDeviceIntoTime())){
            deviceStatus.setDeviceIntoTime(new Timestamp(System.currentTimeMillis()));
        }
        baseMapper.insert(deviceStatus);
        return true;
    }

    /**
    *批量保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public Integer listSave(List<DeviceStatus> deviceStatus) {
        return baseMapper.insertBatchSomeColumn(deviceStatus);
    }

    /**
    * 批量更新
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listUpdate(List<DeviceStatus> deviceStatus) {
        saveOrUpdateBatch(deviceStatus,deviceStatus.size());
    }

    /**
     * 单个更新
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void update(DeviceStatus deviceStatus) {
        LambdaQueryWrapper<DeviceStatus> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DeviceStatus::getDeviceStatusId, deviceStatus.getDeviceStatusId());
        update(deviceStatus, wrapper);
    }

    /**
    * 批量删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listDelete(List<DeviceStatus> deviceStatus) {
        List<String> collect = deviceStatus.stream().map(DeviceStatus::getDeviceId)
                .distinct()
                .collect(Collectors.toList());
        deviceStatusMapper.deleteBatchIds(collect);
    }
    /**
    * 通过id删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public int deleteById(DeviceStatus deviceStatus) {
        if (Common.isNotEmpty(deviceStatus) && Common.isNotEmpty(deviceStatus.getIsDel())) {
            log.info("打印接受的参数{}", JsonUtils.toJsonString(deviceStatus));
            LambdaQueryWrapper<DeviceStatus> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(DeviceStatus::getDeviceStatusId, deviceStatus.getDeviceStatusId());
            update(deviceStatus, wrapper);
            return 0;
        }
        return -1;
    }

    /**
     * 单个查询
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public DeviceStatus queryById(Long deviceId) {
        log.info("打印id" + deviceId);
        LambdaQueryWrapper<DeviceStatus> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DeviceStatus::getDeviceStatusId, deviceId);
        return Common.isEmpty(deviceId) ? null : deviceStatusMapper.selectOne(wrapper);

    }

    /**
     * 分页
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Page queryPage(Page deviceStatusPage, DeviceStatusVO deviceStatusVO) {
        return deviceStatusMapper.selectPage(deviceStatusPage, getWrapper(deviceStatusVO));
    }

    /**
     * 查询一天
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<DeviceStatus> queryList(DeviceStatus query) {
        LambdaQueryWrapper<DeviceStatus> wrapper = Wrappers.lambdaQuery();
        if (Common.isNotNull(query.getDeviceStatusId())) {
            wrapper.eq(DeviceStatus::getDeviceStatusId, query.getDeviceStatusId());
        }
        if (Common.isNotNull(query.getDeviceId())) {
            wrapper.like(DeviceStatus::getDeviceId, "%" + query.getDeviceId() + "%");
        }
        if (Common.isNotNull(query.getDeviceTemperature())) {
            wrapper.eq(DeviceStatus::getDeviceTemperature, query.getDeviceTemperature());
        }
        if (Common.isNotNull(query.getDeviceHumidity())) {
            wrapper.eq(DeviceStatus::getDeviceHumidity, query.getDeviceHumidity());
        }
        if (Common.isNotNull(query.getDeviceBrightness())) {
            wrapper.eq(DeviceStatus::getDeviceBrightness, query.getDeviceBrightness());
        }
        if (Common.isNotNull(query.getDeviceStatus())) {
            wrapper.eq(DeviceStatus::getDeviceStatus, query.getDeviceStatus());
        }
        if (Common.isNotNull(query.getCreateTime())) {
            wrapper.eq(DeviceStatus::getCreateTime, query.getCreateTime());
        }
        if (Common.isNotNull(query.getUpdateTime())) {
            wrapper.eq(DeviceStatus::getUpdateTime, query.getUpdateTime());
        }
        if (Common.isNotNull(query.getCreateor())) {
            wrapper.eq(DeviceStatus::getCreateor, query.getCreateor());
        }
        if (Common.isNotNull(query.getUpdater())) {
            wrapper.eq(DeviceStatus::getUpdater, query.getUpdater());
        }
        /*非空查询*/
        wrapper.eq(DeviceStatus::getIsDel,Constant.NO);
        wrapper.apply("DATE_FORMAT(device_into_time, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')");
        return deviceStatusMapper.selectList(wrapper);
    }

    /**
     * 查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<DeviceStatus> getWrapper(DeviceStatusVO query) {
        LambdaQueryWrapper<DeviceStatus> wrapper = Wrappers.lambdaQuery();
                if(Common.isNotNull(query.getDeviceStatusId())){wrapper.eq(DeviceStatus::getDeviceStatusId,query.getDeviceStatusId());}
                if(Common.isNotNull(query.getDeviceId())){wrapper.eq(DeviceStatus::getDeviceId,query.getDeviceId());}
                if(Common.isNotNull(query.getDeviceTemperature())){wrapper.eq(DeviceStatus::getDeviceTemperature,query.getDeviceTemperature());}
                if(Common.isNotNull(query.getDeviceHumidity())){wrapper.eq(DeviceStatus::getDeviceHumidity,query.getDeviceHumidity());}
                if(Common.isNotNull(query.getDeviceBrightness())){wrapper.eq(DeviceStatus::getDeviceBrightness,query.getDeviceBrightness());}
                if(Common.isNotNull(query.getDeviceStatus())){wrapper.eq(DeviceStatus::getDeviceStatus,query.getDeviceStatus());}
                if(Common.isNotNull(query.getCreateTime())){wrapper.eq(DeviceStatus::getCreateTime,query.getCreateTime());}
                if(Common.isNotNull(query.getUpdateTime())){wrapper.eq(DeviceStatus::getUpdateTime,query.getUpdateTime());}
                if(Common.isNotNull(query.getCreateor())){wrapper.eq(DeviceStatus::getCreateor,query.getCreateor());}
                if(Common.isNotNull(query.getUpdater())){wrapper.eq(DeviceStatus::getUpdater,query.getUpdater());}
        wrapper.eq(DeviceStatus::getIsDel, Constant.NO);
        wrapper.orderByDesc(DeviceStatus::getDeviceId);
    return wrapper;
    }



    /**
    * 返回全部信息
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public List<DeviceStatus> returnAllData() {
        return deviceStatusMapper.selectList(null);
    }

}